﻿<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
			<el-form-item label="标题" prop="title">
			<el-input v-model="dataForm.title" placeholder="标题"></el-input>
		</el-form-item>
		<el-form-item label="作者" prop="author">
			<el-input v-model="dataForm.author" placeholder="作者"></el-input>
		</el-form-item>
		<!-- <el-form-item label="访问量" prop="visited">
			<el-input v-model="dataForm.visited" placeholder="访问量"></el-input>
		</el-form-item> -->
		<!-- <el-form-item label="图片" prop="img">
			<el-input v-model="dataForm.img" placeholder="图片"></el-input>
		</el-form-item> -->
		<!-- <el-form-item label="abstract" prop="abstract">
			<el-input v-model="dataForm.abstract" placeholder="abstract"></el-input>
		</el-form-item> -->
		<el-form-item label="内容" prop="content">
			<!-- <el-input v-model="dataForm.content" placeholder="内容"></el-input> -->
      <wangEditor v-model="dataForm.content" :isClear="isClear"></wangEditor>
		</el-form-item>
		<el-form-item label="分类" prop="category">
			<!-- <el-input v-model="dataForm.category" placeholder="分类"></el-input> -->
      <!-- <el-checkbox-group v-model="dataForm.categoryList">
          <el-checkbox v-for="category in dataForm.categoryList" :key="category.id" :label="category.id">{{ category.name }}</el-checkbox>
        </el-checkbox-group> -->
        
        <el-select  v-model="dataForm.category" placeholder="请选分类">
            <el-option v-for="(item,index) in dataForm.categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
		</el-form-item>
		<el-form-item label="标签" prop="tags">
			<!-- <el-input v-model="dataForm.tags" placeholder="标签"></el-input> -->
      <el-checkbox-group v-model="dataForm.tags">
          <el-checkbox v-for="item in dataForm.tagsList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
        </el-checkbox-group>
		</el-form-item>
		<!-- <el-form-item label="创建时间" prop="createdAt">
			<el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
		</el-form-item>
		<el-form-item label="修改时间" prop="modifiedAt">
			<el-input v-model="dataForm.modifiedAt" placeholder="修改时间"></el-input>
		</el-form-item> -->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import wangEditor from "@/components/wangEditor";
  import { nowtime } from "../../../utils";
  export default {
    components: { wangEditor },
    data () {
      return {
        isClear: false,//设置为true的时候，这个可以用this.wangEditorDetail=''来替代
        visible: false,
        
        dataForm: {
id: 0,
title: '',
author: '',
visited: '',
img: '',
abstract: '',
content: '',
category: '',
tags: [],
createdAt: '',
modifiedAt: '',
categoryList:'',
tagsList:''
        },
        dataRule: {
title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
author: [{ required: true, message: '作者不能为空', trigger: 'blur' }],
// visited: [{ required: true, message: '访问量不能为空', trigger: 'blur' }],
// img: [{ required: true, message: '图片不能为空', trigger: 'blur' }],
// abstract: [{ required: true, message: 'abstract不能为空', trigger: 'blur' }],
content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
category: [{ required: true, message: '分类不能为空', trigger: 'blur' }],
// tags: [{ required: true, message: '标签不能为空', trigger: 'blur' }],
// createdAt: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],
// modifiedAt: [{ required: true, message: '修改时间不能为空', trigger: 'blur' }]

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/blog/article/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
this.dataForm.title = data.article.title
this.dataForm.author = data.article.author
this.dataForm.visited = data.article.visited
this.dataForm.img = data.article.img
this.dataForm.abstract = data.article.abstract
this.dataForm.content = data.article.content
this.dataForm.category = data.article.category
this.dataForm.tags = JSON.parse(data.article.tags)
// console.log("tags:",data.article.tags)
this.dataForm.createdAt = data.article.createdAt
this.dataForm.modifiedAt = data.article.modifiedAt

              }
            })
          }

        })

        this.$http({
          url: this.$http.adornUrl('/blog/category/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dataForm.categoryList = data && data.code === 0 ? data.list : []
          // console.log(this.dataForm.categoryList)
        })
        this.$http({
          url: this.$http.adornUrl('/blog/tags/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.dataForm.tagsList = data && data.code === 0 ? data.list : []
          // console.log(this.dataForm.tagsList)
        })
        if (this.dataForm.id){
          this.dataForm.modifiedAt=nowtime()
        }
        else{
          this.dataForm.author="zyj"
          this.dataForm.visited=1
          this.dataForm.img="xx"
          this.dataForm.abstract="xx"
          this.dataForm.createdAt=nowtime()
          this.dataForm.modifiedAt=nowtime()
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/article/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
'id': this.dataForm.id || undefined,
'title': this.dataForm.title,
'author': this.dataForm.author,
'visited': this.dataForm.visited,
'img': this.dataForm.img,
'abstract': this.dataForm.abstract,
'content': this.dataForm.content,
'category': this.dataForm.category,
'tags': this.dataForm.tags,
'createdAt': this.dataForm.createdAt,
'modifiedAt': this.dataForm.modifiedAt

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
